package com.jinlejia.erp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemLoginExamineBinding;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author SongJiaTai
 * @description:登录审核Adapter
 * @date :2020/7/16 16:24
 */
public class LoginExamineAdapter extends YfListAdapter<Map<String, Object>> {
    private Map<String, Map<String, Object>> selectHistory;

    public LoginExamineAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public void setData(ArrayList<Map<String, Object>> data) {
        if (selectHistory != null && selectHistory.size() > 0) { //如果要填充的数据，有用户所选中的数据，则进行展示
            for (Map<String, Object> map : data) {
                if (Integer.parseInt(String.valueOf(map.get("state"))) == 1) {
                    Map<String, Object> selectedMap = selectHistory.get(String.valueOf(map.get("id")));
                    if (selectedMap != null) {
                        if ("true".equalsIgnoreCase(String.valueOf(selectedMap.get("selected")))) {
                            map.put("selected", String.valueOf(true));
                        }
                    }
                }
            }
        }
        super.setData(data);
    }

    @Override
    public void addData(ArrayList<Map<String, Object>> data) {
        if (selectHistory != null && selectHistory.size() > 0) {
            for (Map<String, Object> map : data) { //如果要填充的数据，有用户所选中的数据，则进行展示
                if (Integer.parseInt(String.valueOf(map.get("state"))) == 1) {
                    Map<String, Object> selectedMap = selectHistory.get(map.get("id"));
                    if (selectedMap != null) {
                        if ("true".equalsIgnoreCase(selectedMap.get("selected").toString())) {
                            map.put("selected", String.valueOf(true));
                        }
                    }
                }
            }
        }
        super.addData(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_login_examine, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemLoginExamineBinding binding = DataBindingUtil.getBinding(holder.itemView);
        Map<String, Object> map = mData.get(position);
        binding.tvApplyTime.setText("申请时间：" + map.get("applyTime"));
        binding.tvName.setText("姓名：" + map.get("name"));
        binding.tvLoginName.setText("登录名：" + map.get("pin"));
        int state = Integer.parseInt(String.valueOf(map.get("state")));
        if (state == 1) {
            binding.tvCheckState.setText("待审核");
            binding.tvCheckState.setTextColor(Color.parseColor("#F0B800"));
            binding.tvTime2.setVisibility(View.GONE);
            binding.idCheckBox.setVisibility(View.VISIBLE);
            binding.idImgNoCheck.setVisibility(View.GONE);
        } else if (state == 2) {
            binding.tvCheckState.setText("已审核");
            binding.tvCheckState.setTextColor(Color.parseColor("#078C26"));
            binding.tvTime2.setText("审核时间：" + map.get("auditTime"));
            binding.tvTime2.setVisibility(View.VISIBLE);
            binding.idCheckBox.setVisibility(View.INVISIBLE);
            binding.idImgNoCheck.setVisibility(View.VISIBLE);
        } else if (state == 3) {
            binding.tvCheckState.setText("已驳回");
            binding.tvCheckState.setTextColor(Color.parseColor("#D50A0A"));
            binding.tvTime2.setText("驳回时间：" + map.get("auditTime"));
            binding.tvTime2.setVisibility(View.VISIBLE);
            binding.idCheckBox.setVisibility(View.INVISIBLE);
            binding.idImgNoCheck.setVisibility(View.VISIBLE);
        }
        int sex = Integer.parseInt(String.valueOf(map.get("sex")));
        if (sex == 1) {
            binding.tvSex.setText("性别：" + "男");
        } else {
            binding.tvSex.setText("性别：" + "女");
        }

        if ("true".equals(map.get("selected"))) {
            binding.idCheckBox.setChecked(true);
        } else {
            binding.idCheckBox.setChecked(false);
        }

        binding.getRoot().setTag(map);
    }

    //region 点击选择调用
    public void addChecked(Map<String, Object> map) {
        if (selectHistory == null) {
            selectHistory = new HashMap<>();
        }
        ArrayList<Map<String, Object>> list = new ArrayList();
        boolean isAllcheck = true;
        if (Integer.parseInt(String.valueOf(map.get("state"))) == 1) {
            boolean selected = Boolean.parseBoolean(String.valueOf(map.get("selected")));
            map.put("selected", String.valueOf(!selected));
            if (!selected) {
                selectHistory.put(String.valueOf(map.get("id")), map);
            } else {
                selectHistory.remove(String.valueOf(map.get("id")));
            }
            //仅对更改的数据进行UI更新
            for (int i = 0; i < mData.size(); i++) {
                Map<String, Object> cuMap = mData.get(i);
                if (TextUtils.equals(String.valueOf(cuMap.get("id")), String.valueOf(map.get("id")))) {
                    cuMap.put("selected", map.get("selected"));
                    notifyItemChanged(i);
                }
                if (Integer.parseInt(String.valueOf(cuMap.get("state"))) == 1) {
                    list.add(cuMap);
                }
            }
            for (int i = 0; i < list.size(); i++) {
                if (!Boolean.parseBoolean(String.valueOf(list.get(i).get("selected")))) {
                    isAllcheck = false;
                }
            }
            mAddCheckLastListener.addcheck(isAllcheck);
        }
    }
    //endregion

    //region 根据推送过来的ID选中数据
    public void addCheckedById(String id) {
        if (!TextUtils.isEmpty(id)){
            for (int i = 0; i < mData.size(); i++) {
                if (Integer.parseInt(id)==Integer.parseInt(String.valueOf(mData.get(i).get("id"))) && Integer.parseInt(String.valueOf(mData.get(i).get("state"))) == 1){
                    mData.get(i).put("selected", "true");
                    selectHistory.put(String.valueOf(mData.get(i).get("id")), mData.get(i));
                    notifyItemChanged(i);
                }
            }
        }
    }
    //endregion

    //region 全选，取消
    public void allChecked(boolean isAllCheck) {
        if (selectHistory == null) {
            selectHistory = new HashMap<>();
        }
        for (Map<String, Object> map : mData) {
            if (Integer.parseInt(String.valueOf(map.get("state"))) == 1) {
                if (isAllCheck) {
                    map.put("selected", "true");
                    selectHistory.put(String.valueOf(map.get("id")), map);
                } else {
                    map.put("selected", "false");
                    selectHistory.remove(String.valueOf(map.get("id")));
                }
            }
        }
        notifyDataSetChanged();
    }
    //endregion


    //region 返回选中数据的id
    public String getSelectId() {
        StringBuilder selectIdString = new StringBuilder();
        for (Map<String, Object> map : mData) {
            if ("true".equalsIgnoreCase(String.valueOf(map.get("selected")))) {
                selectIdString = selectIdString.append(",").append(map.get("id"));
            }
        }
        return selectIdString.toString().replaceFirst(",", "");
    }

    //endregion
    private static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }

    //region 是否手动全选接口回调
    public interface OnaddCheckLatLintener {
        void addcheck(boolean isAllcheck);
    }

    OnaddCheckLatLintener mAddCheckLastListener;

    public void setOnaddLastListener(OnaddCheckLatLintener addCheckLatListener) {
        this.mAddCheckLastListener = addCheckLatListener;
    }
    //endregion

    //reginon 是否有待审核数据
    public boolean isToBeReviewed() {
        boolean isdsh = false;
        for (Map<String, Object> map : mData) {
            if (Integer.parseInt(String.valueOf(map.get("state"))) == 1) {
                isdsh = true;
            }
        }
        return isdsh;
    }
    //endregion
}
